package com.platform.upms.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.platform.common.page.Page;
import com.platform.common.utils.UserUtils;
import com.platform.upms.model.UpmsOrg;
import com.platform.upms.model.ZNode;
import com.platform.upms.model.json.Tip;
import com.platform.upms.service.UpmsOrgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/org")
public class UpmsOrgController {
    private static Logger logger = LoggerFactory.getLogger(UpmsOrgController.class);

    @Autowired
    private UpmsOrgService upmsOrgService;

     /**
     * 组织列表
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(ModelMap modelMap,Page page, UpmsOrg upmsOrg) {
        if(!UserUtils.isHighestPower()) {
            upmsOrg.sethOrgId(UserUtils.getUserOrgId());
        }
        Page<UpmsOrg> upmsOrgs = upmsOrgService.findByPage(page, upmsOrg);
        modelMap.addAttribute("page", upmsOrgs);
        return "/org/list.jsp";
    }

    /**
     * 组织列表的添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "/org/add.jsp";
    }

    /**
     * 添加组织
     * @param upmsOrg
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Tip insert(UpmsOrg upmsOrg){
        Tip tip = new Tip("保存成功");

        if(upmsOrg.getpId() != null) {
            upmsOrg.setCreateTime(new Date());
            if(upmsOrg.getpId() == 0) {
                Integer flag = upmsOrgService.saveOrg(upmsOrg);
                if(flag != 1) {
                    tip = new Tip(1, "数据异常，保存失败");
                }
            } else {
                UpmsOrg uo = upmsOrgService.get(upmsOrg.getpId());
                upmsOrg.sethOrgId(uo.gethOrgId());
                upmsOrgService.saveData(upmsOrg);
            }

        } else {
            tip = new Tip(2, "页面数据异常，请刷新重试");
        }

        return tip;
    }

    /**
     * 进入修改页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Integer id, Model model) {
        UpmsOrg upmsOrg = upmsOrgService.get(id);
        model.addAttribute("org", upmsOrg);
        return "/org/edit.jsp";
    }

    /**
     * 组织基本信息修改
     * @param upmsOrg
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Tip edit(UpmsOrg upmsOrg) {
        Tip tip = new Tip();
        upmsOrgService.update(upmsOrg);
        return tip;
    }

    @ResponseBody
    @RequestMapping(value = "/treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required=false) Integer extId, @RequestParam(required=false) String isShowHide, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<UpmsOrg> list = upmsOrgService.selectAllList(null);
        for (int i=0; i<list.size(); i++){
            UpmsOrg upmsOrg=list.get(i);
            if (extId == null || (extId!=null && !extId.equals(upmsOrg.getId()))){
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", upmsOrg.getId());
                map.put("pId", upmsOrg.getpId());
                map.put("name", upmsOrg.getOrgName());
                mapList.add(map);
            }
        }
        return mapList;
    }

    /**
     * 加载上级组织页面
     * @return
     */
    @RequestMapping(value = "/preOrg")
    public String preOrg() {
        return "/org/pre-org.jsp";
    }

    /**
     * 查询所有组织
     * @return
     */
    @RequestMapping(value = "/getOrgList")
    @ResponseBody
    public List<ZNode> getOrgList() {
        Integer id = null;
        if(!UserUtils.isHighestPower()) {
            id = UserUtils.getUserOrgId();
        }
        List<ZNode> list = upmsOrgService.listOrg(id);
        return list;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Tip delete(Integer[] ids) {
        return upmsOrgService.judgeAnddelete(ids);
    }

    @RequestMapping(value = "/havePowerOrNot")
    @ResponseBody
    public Tip havePowerOrNot(Integer id) {
        Tip tip = new Tip();
        UpmsOrg upmsOrg = upmsOrgService.get(id);
        if(upmsOrg.getpId() == 0) {
            if(!UserUtils.isHighestPower()) {
                tip = new Tip(0, "顶级组织权限不可被修改");
            }
        }
        return tip;
    }

}
